package cn.tqfeiyang.smartadmin.commons.validation;

import cn.hutool.core.lang.Validator;
import cn.tqfeiyang.smartadmin.commons.toolbox.NumberUtils;
import cn.tqfeiyang.smartadmin.commons.toolbox.StringUtils;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

/**
 * 实体验证器
 *
 * @author tqfeiyang
 * @since 2024/10/12 9:49
 */
public class Validators {

    /**
     * 是否是实体id？
     */
    public class EntityIdValidator implements ConstraintValidator<EntityId, Number> {
        @Override
        public boolean isValid(Number value, ConstraintValidatorContext context) {
            return NumberUtils.nonZero(value);
        }
    }

    /**
     * 字符串长度？
     */
    public class MaxLengthValidator implements ConstraintValidator<MaxLength, String> {
        private MaxLength annotation;

        @Override
        public void initialize(MaxLength annotation) {
            this.annotation = annotation;
        }

        @Override
        public boolean isValid(String value, ConstraintValidatorContext context) {
            if (StringUtils.isNotBlank(value) && annotation.length() > 0) {
                return value.length() <= annotation.length();
            }
            return false;
        }
    }

    /**
     * 是否是邮箱地址？
     */
    public class IsEmailValidator implements ConstraintValidator<IsEmail, String> {
        @Override
        public boolean isValid(String value, ConstraintValidatorContext context) {
            return StringUtils.isNotBlank(value) && Validator.isEmail(value);
        }
    }

    /**
     * 是否是手机号码（中国）？
     */
    public class IsMobileValidator implements ConstraintValidator<IsMobile, String> {
        @Override
        public boolean isValid(String value, ConstraintValidatorContext context) {
            return StringUtils.isNotBlank(value) && Validator.isMobile(value);
        }
    }

    /**
     * 是否是URL？
     */
    public class IsUrlValidator implements ConstraintValidator<IsUrl, String> {
        @Override
        public boolean isValid(String value, ConstraintValidatorContext context) {
            return StringUtils.isNotBlank(value) && Validator.isUrl(value);
        }
    }

    /**
     * 是否是中文姓名？
     */
    public class IsChineseNameValidator implements ConstraintValidator<IsChineseName, String> {
        @Override
        public boolean isValid(String value, ConstraintValidatorContext context) {
            return StringUtils.isNotBlank(value) && Validator.isChineseName(value);
        }
    }

    /**
     * 是否是邮政编码（中国）？
     */
    public class IsZipCodeValidator implements ConstraintValidator<IsZipCode, String> {
        @Override
        public boolean isValid(String value, ConstraintValidatorContext context) {
            return StringUtils.isNotBlank(value) && Validator.isZipCode(value);
        }
    }

    /**
     * 是否是身份证号码？
     */
    public class IsCitizenIdValidator implements ConstraintValidator<IsCitizenId, String> {
        @Override
        public boolean isValid(String value, ConstraintValidatorContext context) {
            return StringUtils.isNotBlank(value) && Validator.isCitizenId(value);
        }
    }

    /**
     * 是否是车牌号（中国）？
     */
    public class IsPlateNumberValidator implements ConstraintValidator<IsPlateNumber, String> {
        @Override
        public boolean isValid(String value, ConstraintValidatorContext context) {
            return StringUtils.isNotBlank(value) && Validator.isPlateNumber(value);
        }
    }

    /**
     * 是否是IPV4地址？
     */
    public class IsIpv4Validator implements ConstraintValidator<IsIpv4, String> {
        @Override
        public boolean isValid(String value, ConstraintValidatorContext context) {
            return StringUtils.isNotBlank(value) && Validator.isIpv4(value);
        }
    }

    /**
     * 是否是IPV6地址？
     */
    public class IsIpv6Validator implements ConstraintValidator<IsIpv6, String> {
        @Override
        public boolean isValid(String value, ConstraintValidatorContext context) {
            return StringUtils.isNotBlank(value) && Validator.isIpv6(value);
        }
    }
}
